<?php
namespace Admin\Controller;
class GroupController extends AdminController {
    //菜单列表
    public function index(){
        $data = D('Data')->get_all_data('Group','',10,'id desc');
        $this->assign('_page',$data['_page']);
        $this->assign('data',$data['data']);
        $this->display();
    }
    //菜单添加
    public function add(){
    	if(IS_POST){
            //分组名不能重复
             $group = M('Group')->where(array('group_name'=>I('group_name')))->select();
            if($group){
                $this->error('该分组名已经存在，不可使用');
            }
            $rules = I('rules');
            if(isset($rules)){
                $rules = (array)I('rules');
                sort($rules);
                $str = '';
                foreach($rules as $k => $v){
                    $str.=$v.',';
                }
                $rules = rtrim($str,',');
                $data['rules'] = $rules;
            }
            $data['group_name'] = I('group_name');
            $data['disable'] = intval(I('disable'));
            if(M('Group')->add($data)){
                $this->success('添加成功',U('index'));
            }else{
                $this->error('添加失败');
            }
    	}else{

    		$this->display();
    	}
    }
    //菜单删除
    public function del(){
    	if(D('Group')->data_del()){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败,'.D('Group')->getError());
    	}
    }
   //编辑用户组
    public function edit(){
        if(IS_POST){
            $rules = I('rules');
            if(isset($rules)){
                $rules = (array)I('rules');
                sort($rules);
                $str = '';
                foreach($rules as $k => $v){
                    $str.=$v.',';
                }
                $rules = rtrim($str,',');
                $data['rules'] = $rules;
            }
            $data['group_name'] = I('group_name');
            $data['disable'] = intval(I('disable'));
            $data['id'] = intval(I('id'));
            if(M('Group')->save($data)){
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $group = M('Group') -> find(I('get.id'));
            $str = $group['rules'];
            $rules = array();
            $rules = explode(',', $str);
            $rules = json_encode($rules);
            $this->assign('rules',$rules);
            $this->assign('group',$group);
            $this->display();
        }
    }
}
